package com.itheima.dubbo.mysql.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.model.domain.Admin;
import com.itheima.commons.model.domain.UserFreeze;
import com.itheima.dubbo.interfaces.AdminApi;
import com.itheima.dubbo.interfaces.UserFreezeApi;
import com.itheima.dubbo.mysql.mapper.AdminMapper;
import com.itheima.dubbo.mysql.mapper.UserFreezeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.model.domain.UserFreeze;
import com.itheima.dubbo.interfaces.UserFreezeApi;
import com.itheima.dubbo.mysql.mapper.UserFreezeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.convert.QueryMapper;

import java.util.Date;
import java.util.List;

@Service
public class UserFreezeApiImpl implements UserFreezeApi {

    @Autowired
    private UserFreezeMapper userFreezeMapper;


    @Override
    public UserFreeze queryUserFreezeById(Long userId) {
        UserFreeze userFreeze = userFreezeMapper.selectOne(Wrappers.lambdaQuery(UserFreeze.class)
                .eq(UserFreeze::getUserId, Convert.toInt(userId))
        );
        return userFreeze;
    }

    @Override
    public void freezeUsers(UserFreeze userFreeze) {
        userFreezeMapper.insert(userFreeze);
    }

    @Override
    public void updateFreezeUsers(UserFreeze userFreeze) {
        userFreezeMapper.update(userFreeze, Wrappers.lambdaQuery(UserFreeze.class)
                .eq(UserFreeze::getUserId,userFreeze.getUserId())
        );
    }

    @Override
    public UserFreeze queryUserFreezeByUserId(Integer uid) {
        return userFreezeMapper.selectOne(Wrappers.lambdaQuery(UserFreeze.class)
                .eq(UserFreeze::getUserId, uid));
    }

    /**
     * 查询在管理员ids集合内，在时间区间内，模糊查询理由文本的数据
     *
     * @param adminIds         管理员ids
     * @param endTime          时间区间的结束时间
     * @param startTime        时间区间的开始时间
     * @param operationContent 理由文本
     * @return
     */
    @Override
    public List<UserFreeze> queryByAdminIdsAndTimeAndFreezeRemarks(List<Long> adminIds, Date endTime, Date startTime, String operationContent) {

        List<UserFreeze> userFreezes = userFreezeMapper.selectList(Wrappers.lambdaQuery(UserFreeze.class)
                .in(CollUtil.isNotEmpty(adminIds), UserFreeze::getAdminId, adminIds)
                .like(StrUtil.isNotBlank(operationContent), UserFreeze::getFreezeReason, operationContent)
                .between(UserFreeze::getFreezeStart, startTime, endTime)
        );
        return userFreezes;
    }
        /*@Override
        public UserFreeze queryUserFreezeById (Long userId){

        }
     *//*   *
         *  冻结用户
         * @param userFreeze
         *//*
    @Override
    public void freezeUsers(UserFreeze userFreeze) {
        userFreezeMapper.insert(userFreeze);
    }
*//*
         *
         * 修改用户冻结新信息
         * @param userFreeze
         *//*
    @Override
    public void updateFreezeUsers(UserFreeze userFreeze) {
        userFreezeMapper.update(userFreeze, Wrappers.lambdaQuery(UserFreeze.class)
                        .eq(UserFreeze::getUserId,userFreeze.getUserId())
                        );
    }
    }*/


    @Override
    public void saveUserFreeze(UserFreeze userFreeze) {
        userFreezeMapper.insert(userFreeze);
    }
}
